﻿using System.Threading.Tasks;
using AutoMapper;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using K9Admin.Remoting;
using K9Nano.Admin.Weixin;
using K9Nano.Admin.Weixin.Dto;
using K9Nano.Web.Accounts;
using K9Nano.Web.Accounts.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;

namespace K9Nano.Admin.Account
{
    [Authorize]
    public class UserServiceImplBase : K9Admin.Remoting.UserService.UserServiceBase
    {
        protected readonly IAccountAppService AccountAppService;
        protected readonly IMapper Mapper;

        public UserServiceImplBase(IAccountAppService accountAppService, IMapper mapper)
        {
            AccountAppService = accountAppService;
            Mapper = mapper;
        }

        public override async Task<UUID> Create(UserCreateRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserCreateInput>(request);
            var id = await AccountAppService.CreateAsync(input, context.CancellationToken);
            return new UUID
            {
                Id = id.ToString()
            };
        }

        public override async Task<Empty> AddRoles(UserRoleEditRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserRoleEditInput>(request);
            await AccountAppService.AddRolesAsync(input, context.CancellationToken);
            return new Empty();
        }

        public override async Task<Empty> RemoveRoles(UserRoleEditRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserRoleEditInput>(request);
            await AccountAppService.RemoveRolesAsync(input, context.CancellationToken);
            return new Empty();
        }

        public override async Task<Empty> WeixinUnbind(UserWeixinUnbindRequest request, ServerCallContext context)
        {
            var service = context.GetHttpContext().RequestServices.GetRequiredService<IWeixinAppService>();
            var input = new WeixinMpUnbindModel
            {
                ClientId = request.ClientId,
                UserId = request.UserId
            };
            await service.WeixinUnbindAsync(input, context.CancellationToken);
            return new Empty();
        }

        public override async Task<Empty> Forbid(UserWithClientsRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserWithClientsInput>(request);
            await AccountAppService.ForbidAsync(input, context.CancellationToken);
            return new Empty();
        }

        public override async Task<Empty> Allow(UserWithClientsRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserWithClientsInput>(request);
            await AccountAppService.AllowAsync(input, context.CancellationToken);
            return new Empty();
        }

        public override async Task<Empty> Edit(UserEditRequest request, ServerCallContext context)
        {
            var input = Mapper.Map<UserEditInput>(request);
            await AccountAppService.UpdateBaseInfoAsync(input, context.CancellationToken);
            return new Empty();
        }
    }
}